package com.fenxiangle.yueding.feature.order.view;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.View;
import android.widget.ImageView;

import com.fenxiangle.yueding.R;
import com.fenxiangle.yueding.entity.bo.EventEntity;
import com.fenxiangle.yueding.entity.bo.TabEntity;
import com.fenxiangle.yueding.entity.bo.UnMsgBo;
import com.fenxiangle.yueding.entity.bo.UnReadCountBo;
import com.fenxiangle.yueding.framework.api.HomeApi;
import com.flyco.tablayout.SegmentTabLayout;
import com.flyco.tablayout.listener.CustomTabEntity;
import com.flyco.tablayout.listener.OnTabSelectListener;
import com.luck.picture.lib.rxbus2.RxBus;
import com.suozhang.framework.component.http.RxDataProcessFactory;
import com.suozhang.framework.framework.AM;
import com.suozhang.framework.framework.BaseActivity;
import com.suozhang.framework.utils.logger.Logger;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;
import me.leolin.shortcutbadger.ShortcutBadger;

public class OrderActivity extends BaseActivity implements ReceiveOrderFragment.CallBackValue, OrderFragment.CallBackValue {

    @BindView(R.id.btn_back)
    ImageView mBtnBack;
    private String[] mTitles = {"接单", "订单"};
    @BindView(R.id.tl_5)
    SegmentTabLayout tabLayout_5;
    private ArrayList<CustomTabEntity> mTabEntities;

    private ArrayList<Fragment> mFragments;

    @Override
    protected int attachLayoutRes() {
        return R.layout.activity_order;
    }

    @Override
    protected void initInjector() {

    }

    @Override
    protected void initView() {

        if (!RxBus.getDefault().isRegistered(this)) {
            RxBus.getDefault().register(this);
        }
        mTabEntities = new ArrayList<>();
        mFragments = new ArrayList<>();
        for (int i = 0; i < mTitles.length; i++) {
            mTabEntities.add(new TabEntity(mTitles[i]));
        }

        mFragments.add(new ReceiveOrderFragment());
        mFragments.add(new OrderFragment());
        tabLayout_5.setTabData(mTitles, this, R.id.fl_change, mFragments);

        tabLayout_5.setOnTabSelectListener(new OnTabSelectListener() {
            @Override
            public void onTabSelect(int position) {
                RxBus.getDefault().post(new EventEntity(55));
                getMsgCount();
            }

            @Override
            public void onTabReselect(int position) {

            }
        });
    }

    @Override
    protected void initData() {
        getMsgCount();
    }


    @OnClick(R.id.btn_back)
    public void onViewClicked() {
        finish();
    }

    @Override
    public void SendMessageValue(boolean isStall) {
        if (isStall) {
//            tabLayout_5.setCurrentTab(1);
        }
    }

    @Override
    public void sendSelectCount(boolean isSelect) {
        if (isSelect) {
            getMsgCount();
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (!RxBus.getDefault().isRegistered(this)) {
            RxBus.getDefault().unregister(this);
        }
    }


    private void getMsgCount() {
        AM.api().createApiService(HomeApi.class)
                .selectDiffMessageCount(new UnMsgBo(3 + ""))
                .compose(RxDataProcessFactory.dataPrepAndIoToMainTransformer())
                .subscribe(new Observer<UnReadCountBo>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(UnReadCountBo msgBos) {
                        if (msgBos != null) {
                            if (msgBos.getTotalMyAccessLeft() > 0) {
                                tabLayout_5.showDot(0);
                            }
                            if (msgBos.getTotalMyAccessRight() > 0) {
                                tabLayout_5.showDot(1);
                            }
                            if(msgBos.getUserCenterTotal()>0){
                                ShortcutBadger.applyCount(OrderActivity.this, msgBos.getUserCenterTotal());
                            }else {
                                ShortcutBadger.removeCount(OrderActivity.this);
                            }
                        }
                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    @Override
    public void selectCount(boolean isSelect) {
        if (isSelect) {
            getMsgCount();
        }
    }
}
